package cn.dada.dadarecipes.community.article.conroller;

import cn.dada.dadarecipes.base.response.JsonResult;
import cn.dada.dadarecipes.community.article.pojo.dto.ArticleQuery;
import cn.dada.dadarecipes.community.article.pojo.dto.ArticleSaveParam;
import cn.dada.dadarecipes.community.article.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;

@Api(tags = "02.01文章管理")
@Slf4j
@RestController
@RequestMapping("/v1/article/")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("selectMyArticle")
    public JsonResult selectMyArticle(@NotNull(message = "用户id不能为空") Long userId,HttpSession session) {
        log.debug("业务层-我的文章查询:{}", userId);
        return JsonResult.ok(articleService.selectMyArticle(userId,session));
    }

    @GetMapping("selectLikeArticle")
    public JsonResult selectLikeArticle(@NotNull(message = "用户id不能为空") Long userId, HttpSession session) {
        log.debug("控制器-我的点赞文章查询:{}", userId);
        return JsonResult.ok(articleService.selectLikeArticle(userId,session));
    }

    @GetMapping("selectApproveArticle")
    public JsonResult selectApproveArticle(@NotNull(message = "文章id不能为空") Long userId, HttpSession session) {
        log.debug("控制器-文章点赞查询:{}", userId);
        return JsonResult.ok(articleService.selectApproveArticle(userId,session));
    }

    /**
     * 控制器-首页文章查询
     * @param articleQuery
     * @return 2000-List<ArticleVO> 2001-操作失败
     */
    @GetMapping("select")
    @ApiOperation("首页文章查询")
    public JsonResult select(ArticleQuery articleQuery) {
        log.debug("控制器-首页查询文章:{}", articleQuery);
        return JsonResult.ok(articleService.selectArticle(articleQuery));
    }

    /**
     * 控制器-文章详情查询
     * @param articleId
     * @return 2000-ArticleDetailsVO 2001-文章id不能为空 4000-文章不存在
     */
    @GetMapping("details/select")
    @ApiOperation("文章详情查询")
    @ApiImplicitParam(name = "articleId", value = "文章id", required = true)
    public JsonResult selectDetails(
            @NotNull(message = "文章id不能为空") @RequestParam("articleId") Long articleId,
            @RequestParam("userId") Long userId,
            @ApiIgnore HttpSession session) {
        log.debug("控制器-查询文章详情:articleId={},userId={}", articleId,userId);
        return JsonResult.ok(articleService.selectArticleDetails(articleId,userId,session));
    }

    /**
     * 控制器-新增与更新文章
     * @param articleSaveParam
     * @return 2000-操作成功 2001-操作失败
     */
    @PostMapping("insert")
    @ApiOperation("新增与更新文章")
    public JsonResult insert(
            @Validated ArticleSaveParam articleSaveParam,
            @ApiIgnore HttpSession session) {
        log.debug("控制器-新增与更新文章:{},{}", articleSaveParam,session);
        Long id = articleService.saveArticle(articleSaveParam, session);
        return JsonResult.ok(id);
    }

    /**
     * 控制器-删除文章
     * @param articleId
     * @return
     */
    @PostMapping("delete")
    @ApiOperation("删除文章")
    @ApiImplicitParam(name = "articleId", value = "文章id", required = true)
    public JsonResult delete(
            @NotNull(message = "文章id不能为空") @RequestParam Long articleId,
            @ApiIgnore HttpSession session) {
        log.debug("控制器-删除文章:{}", articleId);
        articleService.deleteArticle(articleId,session);
        return JsonResult.ok();
    }
}
